package com.pxmeta.common.utils;

import org.springframework.core.io.ClassPathResource;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;

public class IOUtils {

    public static String  getResourcesByStream(String fileName) {
        String str = "";
        ClassPathResource resource = new ClassPathResource(fileName);
        try {
            InputStream inputStream = resource.getInputStream();
            //将流转为字符串
            str = getStringByInputStream(inputStream);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return str;
    }



    public static String getStringByInputStream(InputStream inputStream) {
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        try {
            byte[] b = new byte[10240];
            int n;
            while ((n = inputStream.read(b)) != -1) {
                outputStream.write(b, 0, n);
            }
        } catch (Exception e) {
            try {
                inputStream.close();
                outputStream.close();
            } catch (Exception e1) {
            }
        }
        return outputStream.toString();
    }
}
